//
//  EssentialInformation.m
//  BusinessApp
//
//  Created by CccDaxIN on 2016/10/14.
//  Copyright © 2016年 macox. All rights reserved.
//

#import "EssentialInformation.h"
#import "OrderDetailsModel.h"

@implementation EssentialInformation
- (id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        _essential_array = [[NSMutableArray alloc]init];
        self.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.94 alpha:1.00];
        [SVProgressHUD showWithStatus:@"正在加载"];
        [self createSubView];
    }
    return self;
}
#pragma mark -- 创建试图
- (void)createSubView{
    _recordState = @"1";
    
    /*
     价格背景view
     */
    UIView *priceView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScrernWidth, 50)];
    priceView.backgroundColor = [UIColor whiteColor];
    [self addSubview:priceView];
    
    NSArray *pricr_array = @[@"总价:",@"实际金额:"];
    for (int i = 0; i < 2 ; i++) {
        UILabel *priceName = [[UILabel alloc]initWithFrame:CGRectMake(20, 27*i, 70, 20)];
        priceName.text = [pricr_array objectAtIndex:i];
        priceName.font = [UIFont systemFontOfSize:12];
        priceName.textColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.28 alpha:1.00];
        [priceView addSubview:priceName];
    }
    /*
     *创建赋值label
     */
    _totalLabel = [[UILabel alloc]initWithFrame:CGRectMake(150, 0, kScrernWidth-160, 20)];
    _totalLabel.textAlignment = NSTextAlignmentRight;
    _totalLabel.font = [UIFont systemFontOfSize:12];
    _totalLabel.textColor = [UIColor colorWithHue:0.10 saturation:1.00 brightness:0.91 alpha:1.00];
    [priceView addSubview:_totalLabel];

//    _redLabel = [[UILabel alloc]initWithFrame:CGRectMake(150, 21, kScrernWidth-160, 20)];
//    _redLabel.textAlignment = NSTextAlignmentRight;
//    _redLabel.font = [UIFont systemFontOfSize:12];
//    _redLabel.textColor = [UIColor colorWithHue:0.10 saturation:1.00 brightness:0.91 alpha:1.00];

//    [priceView addSubview:_redLabel];
    
    _actualPayAmount = [[UILabel alloc]initWithFrame:CGRectMake(150, 27, kScrernWidth-160, 20)];
    _actualPayAmount.textAlignment = NSTextAlignmentRight;
    _actualPayAmount.font = [UIFont systemFontOfSize:12];
    _actualPayAmount.textColor = [UIColor colorWithHue:0.10 saturation:1.00 brightness:0.91 alpha:1.00];

    [priceView addSubview:_actualPayAmount];
    
    NSArray *array = @[@"基本信息",@"收货信息"];
    NSInteger informationTag = 1222;
    for (int i = 0; i < 2; i++) {
        _informationButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_informationButton addTarget:self action:@selector(informationButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        _informationButton.backgroundColor = [UIColor orangeColor];
        [_informationButton setTitle:[array objectAtIndex:i] forState:UIControlStateNormal];
        _informationButton.tag = informationTag++;
        _informationButton.titleLabel.font = [UIFont systemFontOfSize: 13];
        [_informationButton  setTitleColor:[UIColor colorWithHue:0.21 saturation:1.00 brightness:0.69 alpha:1.00] forState:UIControlStateNormal];
        _informationButton.frame = CGRectMake(kScrernWidth/2*i,51,kScrernWidth/2,35);
        _informationButton.backgroundColor = [UIColor whiteColor];
        [self addSubview:_informationButton];
    }
    /*
     竖
     */
    UIView *uprightView = [[UIView alloc]initWithFrame:CGRectMake(kScrernWidth/2, 51, 1, 35)];
    uprightView.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.90 alpha:1.00];
    [self addSubview:uprightView];
    /*
     *横
     */
    _transverseView = [[UIView alloc]initWithFrame:CGRectMake(0, 31, kScrernWidth/2, 5)];
    _transverseView.backgroundColor = [UIColor colorWithHue:0.21 saturation:1.00 brightness:0.69 alpha:1.00];
    _informationButton = (UIButton *)[self viewWithTag:1222];
    [_informationButton addSubview:_transverseView];
    [self orderInformationSubView];

    
}
#pragma mark -- 创建订单信息
- (void)orderInformationSubView{
    NSMutableArray *order_array = [[NSMutableArray alloc]initWithObjects:@"订单号：",@"订单状态：",@"卖家昵称：",@"下单时间：",@"支付时间：", nil];
    NSMutableArray *recipient_array = [[NSMutableArray alloc]initWithObjects:@"收货人：",@"手机号：",@"收货地址：",@"发货时间：", nil];
    
    if ([_recordState isEqualToString:@"1"]) {
        [recipient_array removeAllObjects];
        [_whiteViewTwo removeFromSuperview];
        [_labelTwo removeFromSuperview];
        for (int i = 0; i < 5; i++) {
              _whiteViewOne = [[UIView alloc]initWithFrame:CGRectMake(0, 41*i+89, kScrernWidth, 40)];
            _whiteViewOne.backgroundColor = [UIColor whiteColor];
            [self addSubview:_whiteViewOne];
            _labelOne = [[UILabel alloc]initWithFrame:CGRectMake(15, 41*i+98, 70, 20)];
            _labelOne.text = [order_array objectAtIndex:i];
            _labelOne.font = [UIFont systemFontOfSize:13];
            [self addSubview:_labelOne];
        }
        /*
         订单号
         */
        _orderNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 98, kScrernWidth-90, 20)];
        _orderNumberLabel.font = [UIFont systemFontOfSize:13];
        [self addSubview:_orderNumberLabel];
        /*
         订单状态
         */
        _orderStatusLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 139, kScrernWidth-90, 20)];
        _orderStatusLabel.font = [UIFont systemFontOfSize:13];
        [self addSubview:_orderStatusLabel];
        /*
         卖家昵称
         */
        _sellerNicknameLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 179, kScrernWidth-90, 20)];
        _sellerNicknameLabel.font = [UIFont systemFontOfSize:13];
        [self addSubview:_sellerNicknameLabel];
        /*
         下单时间
         */
        _orderTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 220, kScrernWidth-90, 20)];
        _orderTimeLabel.font = [UIFont systemFontOfSize:13];
        [self addSubview:_orderTimeLabel];
        /*
         支付时间
         */
        _paymentTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 261, kScrernWidth-90, 20)];
        _paymentTimeLabel.font = [UIFont systemFontOfSize:13];
        [self addSubview:_paymentTimeLabel];
        
    }else{
        [order_array removeAllObjects];
        [_whiteViewOne removeFromSuperview];
        [_labelOne removeFromSuperview];
        [_orderNumberLabel removeFromSuperview];
        [_orderTimeLabel removeFromSuperview];
        [_orderStatusLabel removeFromSuperview];
        [_sellerNicknameLabel removeFromSuperview];
        [_paymentTimeLabel removeFromSuperview];
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 89, kScrernWidth,400)];
        view.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.94 alpha:1.00];
        [self addSubview:view];
        for (int i = 0; i < 4; i++) {
           _whiteViewTwo = [[UIView alloc]initWithFrame:CGRectMake(0, 41*i+0, kScrernWidth, 40)];
            _whiteViewTwo.backgroundColor = [UIColor whiteColor];
            [view addSubview:_whiteViewTwo];
            
            _labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(15, 41*i+10, 70, 20)];
            _labelTwo.text = [recipient_array objectAtIndex:i];
            _labelTwo.font = [UIFont systemFontOfSize:13];
            [view addSubview:_labelTwo];
        }
        
        /*
         收货人
         */
        _consigneeLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 99, kScrernWidth-90, 20)];
        _consigneeLabel.font = [UIFont systemFontOfSize:13];
        [self addSubview:_consigneeLabel];
        /*
          手机号
         */
        _iphoneNumber = [[UILabel alloc]initWithFrame:CGRectMake(80, 140, kScrernWidth-90, 20)];
        _iphoneNumber.font = [UIFont systemFontOfSize:13];
        [self addSubview:_iphoneNumber];
        /*
         收货地址
         */
        _receiptAddressLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 180, kScrernWidth-90, 20)];
        _receiptAddressLabel.font = [UIFont systemFontOfSize:13];
        [self addSubview:_receiptAddressLabel];
        
        /*
         发货时间
         */
        _receivingTime = [[UILabel alloc]initWithFrame:CGRectMake(80, 221, kScrernWidth-90, 20)];
        _receivingTime.font = [UIFont systemFontOfSize:13];
        [self addSubview:_receivingTime];
    }
}
#pragma mark -- 基本信息 or 收货信息
- (void)informationButtonAction:(UIButton *)sender{
    switch (sender.tag) {
        case 1222:{
            NSLog(@"基本信息");
            [self displayDetailDataRequest];
            _recordState = @"1";
            [UIView animateWithDuration:5 animations:^{
                _informationButton = (UIButton *)[self viewWithTag:1222];
                _transverseView.frame = CGRectMake(0, 31, kScrernWidth/2, 5);
            }];
            [_informationButton addSubview:_transverseView];
            [self orderInformationSubView];
        }
            break;
        case 1223:{
            NSLog(@"收货信息");
            [NNSingleton sharedSingleton].ShiJiDaoZhangJinE = @"2";
            [self displayDetailDataRequest];
            _recordState= @"2";
            [UIView animateWithDuration:5 animations:^{
            _informationButton = (UIButton *)[self viewWithTag:1223];
            _transverseView.frame = CGRectMake(0, 31, kScrernWidth/2, 5);
            }];
            [_informationButton addSubview:_transverseView];
            [self orderInformationSubView];
        }
            break;
        default:
            break;
    }
}
#pragma mark -- 展示数据
- (void)displayDetailDataRequest{
    [GDJHttpTool get:Orderdetails params:@{@"ShouJiHao":GETPHONE,@"Duuid":GETUUID,@"Token":GETTOKEN,@"OrderId":[NNSingleton sharedSingleton].commodityID} success:^(id responseObj) {
        NSLog(@"喜欢你 %@",responseObj[@"Order"]);
        //查看错误码
        NSInteger err = [[responseObj valueForKey:@"Err"]integerValue];
        NSLog(@"哈哈哈 %ld",(long) err);
        if (err == 0) {
            [SVProgressHUD dismiss];
            _essential_array =   [OrderDetailsModel mj_objectArrayWithKeyValuesArray:responseObj[@"Order"]];
            
            [self displayDataSubView];
        }
    }failure:^(NSError *error) {
    }];
}
- (void)displayDataSubView{
    
    OrderDetailsModel *model = [_essential_array objectAtIndex:0];

    
    /*
     订单号
     */
    NSString *string = [NSString stringWithFormat:@"%@",model.OrderCode];
    self.orderNumberLabel.text = string;
    /*
     订单状态
     */
    NSString *string_statues = [NSString stringWithFormat:@"%d",model.OrderStatues];
    if ([string_statues isEqualToString:@"1"]) {
        self.orderStatusLabel.text = @"待付款";
    }else if ([string_statues isEqualToString:@"4"]){
        self.orderStatusLabel.text = @"已关闭";
    }else if ([string_statues isEqualToString:@"5"]){
        self.orderStatusLabel.text = @"已完成";
        if ([_recordState isEqualToString:@"1"]) {
            _actualArrivalView = [[UIView alloc]initWithFrame:CGRectMake(0, 294, kScrernWidth, 40)];
            _actualArrivalView.backgroundColor = [UIColor whiteColor];
            [self addSubview:_actualArrivalView];
            
            UILabel *labelTitle = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 100, 20)];
            labelTitle.text = @"实际到账金额：";
            labelTitle.font = [UIFont systemFontOfSize:13];
            [_actualArrivalView addSubview:labelTitle];
            /*
             *实际到帐金额
             */
            UILabel *dataLabel = [[UILabel alloc]initWithFrame:CGRectMake(105, 10, kScrernWidth-100, 20)];
            NSString *stringTotalAmount = [NSString stringWithFormat:@"%.2f元",model.ShiJiDaoZhangJinE];
            dataLabel.font = [UIFont systemFontOfSize:13];
            dataLabel.text = stringTotalAmount;
            [_actualArrivalView addSubview:dataLabel];
        }
    }else if ([string_statues isEqualToString:@"7"]){
        self.orderStatusLabel.text = @"未评价";
    }else if ([string_statues isEqualToString:@"2"]){
        self.orderStatusLabel.text = @"待发货";
    }else if ([string_statues isEqualToString:@"3"]){
        self.orderStatusLabel.text = @"待收货";
    }
    /*
     卖家昵称
     */
    self.sellerNicknameLabel.text = model.UserName;
    /*
     下单时间
     */
    self.orderTimeLabel.text = model.OrderDate;
    /*
     支付时间
     */
    self.paymentTimeLabel.text = model.PayDate;
    
    //收货人
    self.consigneeLabel.text = model.ShipTo;
    //手机号
    self.iphoneNumber.text = model.Phone;
    //地址
    self.receiptAddressLabel.text = model.Address;
    //发货时间
    self.receivingTime.text = model.ShippingDate;
 
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
